$(function () {
    $(".seckill").click(function () {
        var productId = $(this).attr("id");
        $.ajax({
            url:'/seckill/getLoginUserId',
            dataType:'json',
            success:function (result) {
                console.log(result);
                if(result.code == 500) {
                    alert('请先登录');
                    location.href='/user/login';
                } else {
                    $.ajax({
                        url:'/seckill/seckill?userId='+result.data+"&productId=" + productId,
                        dataType:'json',
                        success:function (r) {
                        	alert(r.msg);
                            if(r.code==200) {
                                //跳转的订单页面
                                //location.href = '/order/createSeckillOrder?userId='+result.data+"&productId=" + productId;
                                //跳转的等待订单页面
                                location.href = '/order/waiting?productId='+productId;
                            }
                        }
                    })
                }
            },
            error:function (xhr) {
                console.log(xhr);
                alert('出错了')
            }
        })

    });//点击秒杀按钮结束
    //
    var timerHandler = [];
    /*$("p.time").each(function (i, e) {
        var t = $(this).attr('data');
        var $that = $(this);
        console.log(t+"," + i+"--" + e);
        //t = t - 8 * 3600 * 1000;
        timerHandler[i] = setInterval(function () {
            // 剩余时间：设定的-当前的
            var leftTime = (new Date(t)) - (new Date());
            // parseInt()返回一个整数。得出剩余的时分秒
            var days = parseInt(leftTime / 1000 / 60 / 60 / 24, 10);
            var hours = parseInt(leftTime / 1000 / 60 / 60 % 24, 10);
            var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);
            var seconds = parseInt(leftTime / 1000 % 60, 10);
            // 结束的时候
            if (seconds < 0) {
                clearInterval(timerHandler[i]);
                console.log('秒杀开始')
                $that.prev('button').attr('disabled','');
            }
            else {
                // 格式的转化
                days = fix(days, 2);
                hours = fix(hours, 2);
                minutes = fix(minutes, 2);
                seconds = fix(seconds, 2);
                // // 递归调用 注意：比当前时间大！
                // timer = setTimeout('miaosha(2019,09,05,10,56,00)', 1000); //// 设置开始的时间
                // 设置时分秒
                $that.children("span:eq(0)").html(days);
                $that.children("span:eq(1)").html(hours);
                $that.children("span:eq(2)").html(minutes);
                $that.children("span:eq(3)").html(seconds);
            }
            t = t - 1000;
        },1000);
    })*/
});

//fix函数：数字加0
function fix(num, length) {
    // console.log(num);
    // 数字转化为字符串 进行拼接
    return num.toString().length<length?'0'+num:num;
}